/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.fatecpg.songs.intranet.managedbeans;

import br.com.fatecpg.songs.crud.facade.RegisterFacade;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.entity.Genre;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author Eloyza
 */
@ManagedBean
@ViewScoped
public class GenreManagedBean implements RegisterManagedBean<Genre>{

    private Genre genre;
    private RegisterFacade<Genre> facade = new RegisterFacade<Genre>(Genre.class);    
    private int viewState;  
    private List<Genre> list;        
        
    public GenreManagedBean() throws DAOException{
        genre = new Genre();        
        list = facade.list();
        this.viewState = LIST_STATE;
    }    

    @Override
    public void openNew() {
        genre = new Genre();
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void openToUpdate() throws DAOException {        
        genre = facade.load();
        this.viewState = REGISTER_STATE;
    }

    @Override
    public void delete() throws DAOException {
        try{
            facade.delete();
            list = facade.list();
            this.viewState = LIST_STATE;            
        }catch(ConstraintViolationException ex){
             FacesContextUtil.addMessage("Existem músicas e/ou artistas deste gênero. Para deletá-lo, é necessário deletar as músicas e os artistas.");
        }        
    }


    @Override
    public void register() throws DAOException {
        facade.register(genre);
        list = facade.list();
        viewState = LIST_STATE;
    }

    @Override
    public List<Genre> getList() {
        return list;
    }

    public void setList(List<Genre> list) {
        this.list = list;
    }
    
    @Override
    public void cancel() {
        this.viewState = LIST_STATE;
    }      
    
    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    public int getViewState() {
        return viewState;
    }

    public void setViewState(int viewState) {
        this.viewState = viewState;
    }
}
